<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active : active }">
      <span v-if="!changeResult.resultAddress" class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{changeResult.resultAddress}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span @click="optionClick(i)" class="ellipsis" v-for="i in cityList" :key="i.code">{{i.name}}</span>
    </div>
  </div>
</template>

<script>
import { ref, reactive, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  setup (props, { emit }) {
    // 控制展开收起,默认收起
    const active = ref(false)
    // 打开弹框
    function open () {
      active.value = true
    }
    // 关闭弹框
    function close () {
      active.value = false
    }
    // 切换展开收起
    function toggleDialog () {
      if (active.value) {
        close()
      } else {
        open()
      }
    }

    // 获取城市数据
    const cityList = ref([])
    // 定义一个备份的城市数据
    const cityCacheList = ref([])
    const loadCityList = async () => {
      const res = await axios({
        method: 'get',
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json',
      })
      console.log('城市数据', res)
      cityList.value = res.data
      // 备份引用类型的数据时 最好深拷贝一下
      cityCacheList.value = JSON.parse(JSON.stringify(res.data))
    }
    loadCityList()

    // 每一项都有一个areaList 字段存着子集的列表  省-> 市 -> 区
    // 每一个项都有一个level  0 省  1 市 2 区
    // 3. 交互选择
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '', // 地区名
      resultAddress: '' // 最终结果地址
    })
    // 思路:每一次点击的时候会 用areaList替换cityList
    const optionClick = (i) => {
      // 第一次点击的时候 level 是 0  是省级
      if (i.level === 0) {
        changeResult.provinceCode = i.code
        changeResult.provinceName = i.name
      }
      // 第二次点的时候  level 是1  是市级
      if (i.level === 1) {
        changeResult.cityCode = i.code
        changeResult.cityName = i.name
      }
      console.log('我是被点击的那一项', i)
      // 点击到区一级的时候需要关闭弹窗  level === 2
      if (i.level === 2) {
        changeResult.countyCode = i.code
        changeResult.countyName = i.name
        changeResult.resultAddress = changeResult.provinceName + '-' + changeResult.cityName +
        '-' + changeResult.countyName
        console.log('changeResult', changeResult)
        emit('change', changeResult)
        close()
      }
      cityList.value = i.areaList
    }

    // 监听active  每次active变成true的时候还原一下cityList
    watch(active, newVal => {
      if (newVal === true) {
        cityList.value = JSON.parse(JSON.stringify(cityCacheList.value))
        changeResult.provinceCode = ''
        changeResult.provinceName = ''
        changeResult.cityCode = ''
        changeResult.cityName = ''
        changeResult.countyCode = ''
        changeResult.countyName = ''
        changeResult.resultAddress = ''
      }
    })

    return { active, cityList, changeResult, toggleDialog, optionClick }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
